package com.example.hubeiweb.tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCTool {

    public static Connection getConnection() throws Exception {
        try {
            // 加载驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.err.println("驱动程序未找到！");
            throw e;
        }

        String username = "root";
        String password = "20040502hzh";
        String url = "jdbc:mysql://localhost:3306/day2"
                + "?characterEncoding=UTF-8"
                + "&useSSL=false"
                + "&useUnicode=true"
                + "&serverTimezone=Asia/Shanghai"
                + "&allowPublicKeyRetrieval=true";

        Connection conn = null;
        try {
            // 建立连接
            conn = DriverManager.getConnection(url, username, password);
            System.out.println("数据库连接成功！");
        } catch (SQLException e) {
            System.err.println("数据库连接失败！");
            throw e;
        }
        return conn;
    }
}